Telegram Group & Telegram Channel
🌀 Middleware и RoundTripper в Go: гибкая обработка HTTP-запросов

В Go нет встроенного понятия "middleware", как в других фреймворках — но вы можете легко реализовать его сами, особенно в контексте http.RoundTripper.

🔧 RoundTripper — это интерфейс, который обрабатывает HTTP-запросы на уровне клиента (`http.Client`). Это даёт возможность внедрять логику *до* и *после* отправки запроса.

🧱 Пример: логгер запросов

type LoggingRoundTripper struct {
rt http.RoundTripper
}

func (l *LoggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
log.Printf("📤 Запрос: %s %s", req.Method, req.URL)
resp, err := l.rt.RoundTrip(req)
if err == nil {
log.Printf("📥 Ответ: %d", resp.StatusCode)
}
return resp, err
}


📦 Использование:

client := &http.Client{
Transport: &LoggingRoundTripper{rt: http.DefaultTransport},
}


💡 Таким образом, вы можете создавать middleware прямо внутри http.Client, например:
• логирование
• кэширование
• модификация заголовков
• retry-логика
• трейсинг

🔗 Подробнее: dev.to/calvinmclean/middleware-and-roundtrippers-in-go-30pa

#golang #httpclient #middleware #webdev #devtools



tg-me.com/golang_books/994
Create:
Last Update:

🌀 Middleware и RoundTripper в Go: гибкая обработка HTTP-запросов

В Go нет встроенного понятия "middleware", как в других фреймворках — но вы можете легко реализовать его сами, особенно в контексте http.RoundTripper.

🔧 RoundTripper — это интерфейс, который обрабатывает HTTP-запросы на уровне клиента (`http.Client`). Это даёт возможность внедрять логику *до* и *после* отправки запроса.

🧱 Пример: логгер запросов


type LoggingRoundTripper struct {
rt http.RoundTripper
}

func (l *LoggingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
log.Printf("📤 Запрос: %s %s", req.Method, req.URL)
resp, err := l.rt.RoundTrip(req)
if err == nil {
log.Printf("📥 Ответ: %d", resp.StatusCode)
}
return resp, err
}


📦 Использование:

client := &http.Client{
Transport: &LoggingRoundTripper{rt: http.DefaultTransport},
}


💡 Таким образом, вы можете создавать middleware прямо внутри http.Client, например:
• логирование
• кэширование
• модификация заголовков
• retry-логика
• трейсинг

🔗 Подробнее: dev.to/calvinmclean/middleware-and-roundtrippers-in-go-30pa

#golang #httpclient #middleware #webdev #devtools

BY Golang Books




Share with your friend now:
tg-me.com/golang_books/994

View MORE
Open in Telegram


Golang Books Telegram | DID YOU KNOW?

Date: |

To pay the bills, Mr. Durov is issuing investors $1 billion to $1.5 billion of company debt, with the promise of discounted equity if the company eventually goes public, the people briefed on the plans said. He has also announced plans to start selling ads in public Telegram channels as soon as later this year, as well as offering other premium services for businesses and users.

Look for Channels Online

You guessed it – the internet is your friend. A good place to start looking for Telegram channels is Reddit. This is one of the biggest sites on the internet, with millions of communities, including those from Telegram.Then, you can search one of the many dedicated websites for Telegram channel searching. One of them is telegram-group.com. This website has many categories and a really simple user interface. Another great site is telegram channels.me. It has even more channels than the previous one, and an even better user experience.These are just some of the many available websites. You can look them up online if you’re not satisfied with these two. All of these sites list only public channels. If you want to join a private channel, you’ll have to ask one of its members to invite you.

Golang Books from es


Telegram Golang Books
FROM USA